package com.zkingsoft.actions.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.zkingsoft.model.Btn;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BaseServices;

public abstract class BaseController {
	public static final Logger log = Logger.getLogger(BaseController.class);

	public <T> AjaxResult add(BaseServices<T> baseServices, T t, String name) {
		int i = baseServices.add(t);
		if (i != 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, name + "添加成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "添加失败");
		}
	}

	public <T> AjaxResult modify(BaseServices<T> baseServices, T t, String name) {
		int i = baseServices.modify(t);
		if (i != 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, name + "修改成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "修改失败");
		}
	}

	public <T> AjaxResult remove(BaseServices<T> baseServices, String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = baseServices.remove(ids);
		if (i != 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "删除失败");
		}
	}

	public <T> AjaxResult showList(BaseServices<T> baseServices, T t, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		pageVo.setTotalRecold(baseServices.findTotal(t));
		List<T> dataList = baseServices.findInPage(t, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, pageVo.getTotalRecold(),
				pageVo.getPageCount());
		return result;
	}
	
	
	public <T> AjaxResult findByModel(BaseServices<T> baseServices,T t) {
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, baseServices.findByModel(t),0,0);
		return result;
	}
	public <T> T findById(BaseServices<T> baseServices,Long id) {
		return baseServices.findById(id);
	}
	
	public Users getMe(HttpServletRequest request){
		return (Users)request.getSession().getAttribute("user");
	}

}
